//
//  PZRClickViewController.m
//  Moment
//
//  Created by pang on 16/1/4.
//  Copyright © 2016年 片刻. All rights reserved.
//

#import "PZRClickViewController.h"
#import "PZRClick1.h"
#import "PZRClickTableViewCell.h"
#import "PZRClickDetailViewController.h"
@interface PZRClickViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) UITableView  *neWTableView;
@property (nonatomic,strong) NSMutableArray *allArray;
@property (nonatomic, assign) NSInteger nextPage;
@property (nonatomic, assign) BOOL isUpLoad;  /**< 标记上拉操作还是下拉操作，yes为上拉 */

//@property (nonatomic,strong) UITableView  *hotTableView;
@end

@implementation PZRClickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    [self createNewTableView];
    self.nextPage = 0;
    [self addHeaderRefresh];
    [self addFooterRefresh];
    _isUpLoad = NO;

}

-(void)addHeaderRefresh
{
    
    //当下拉时候会触发该方法
    self.neWTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉");
        //        self.nextPage = 0;// 记录页码的
        self.isUpLoad = NO;//标记为下拉操作
        self.nextPage = 0;
        // 重新请求数据
        [self getData];
    }];
    
    // 进到应用自动刷新(可以放在viewdidload)
    [self.neWTableView.mj_header beginRefreshing];
}

#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    //    设置上拉刷新动画
    MJRefreshAutoGifFooter  *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    //[footer setImages:_refreshPicArr forState:MJRefreshStateRefreshing];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    
    self.neWTableView.mj_footer = footer;
    [self.neWTableView.mj_footer beginRefreshing];
}
-(void)refreshAction{
    NSLog(@"上拉");
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingDataIsUpLoad];
}



-(void)getData
{
    self.allArray = [NSMutableArray array];
    NSString *url = @"http://api2.pianke.me/read/columns_detail";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&sort=addtime&start=%ld&typeid=%@&version=3.0.6",(long)self.nextPage,self.type];
    [GetData postUrl:url body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            PZRClick1 *click1 = [[PZRClick1 alloc]init];
            [click1 setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:click1];
     
        
        }
        [self.neWTableView reloadData];
        [self.neWTableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];

    
}
- (void)getAFNetWorkingDataIsUpLoad{
    //self.allArray = [NSMutableArray array];
    NSString *url = @"http://api2.pianke.me/read/columns_detail";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&sort=addtime&start=%ld&typeid=%@&version=3.0.6",(long)self.nextPage,self.type];
    [GetData postUrl:url body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            PZRClick1 *click1 = [[PZRClick1 alloc]init];
            [click1 setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:click1];
            
            
        }
        
        [self.neWTableView.mj_footer endRefreshing];
        [self.neWTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
    
}

-(void)createNewTableView
{
    self.neWTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    //self.neWTableView.backgroundColor = [UIColor redColor];
    self.neWTableView.delegate = self;
    self.neWTableView.dataSource  =self;
    [self.neWTableView registerClass:[PZRClickTableViewCell class] forCellReuseIdentifier:@"new"];
    [self.view addSubview:_neWTableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *chongyong = @"new";
    PZRClickTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong];
    PZRClick1 *click = [[PZRClick1 alloc]init];
    click = [self.allArray objectAtIndex:indexPath.row];
    cell.aClick1 = click;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PZRClick1 *click1 = [[PZRClick1 alloc]init];
    click1 = [self.allArray objectAtIndex:indexPath.row];
    PZRClickDetailViewController *clickVC = [[PZRClickDetailViewController alloc]init];
    clickVC.contentId = click1.contentId;
    clickVC.titleStr = click1.title;
    [self.navigationController pushViewController:clickVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
